#include "VHistory.h"
#include "ui_VHistory.h"
#include "VHistorySession.h"
#include <QGraphicsDropShadowEffect>
#include <QLabel>
#include "CMainWindow.h"
#include "CHelper.h"
#include <QMessageBox>
#include "CTableConfig.h"

VHistory::VHistory(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VHistory)
{
    ui->setupUi(this);

    //标题属性
    ui->label_history->setProperty("title","h1");
    init();
}

VHistory::~VHistory()
{
    delete ui;
}

void VHistory::init()
{

}

void VHistory::refreshUI()
{
    //清除旧的 @MAJR 需优化清除方式
    QLayoutItem *child;
    while ((child = ui->verticalLayout_sessions->takeAt(0)) != 0) {
        if(child->widget())
            child->widget()->deleteLater(); //注意如果有残影 使用delete
        delete child;
    }

    foreach(QObject *pChild,ui->widget_sessions->children()){
        if(pChild != ui->verticalLayout_sessions)
            pChild->deleteLater();
    }

    //重新获取
    //建立组和组名称的映射
    static QMap<SessionGroup,QString> mapGroupName = {{Today,tr("今天")},
                                           {ThisWeek,tr("一周内")},
                                           {ThisMonth,tr("一月内")},
                                           {ThisYear,tr("一年内")},
                                           {Other,tr("其他时间")}};

    //按照今天、本周、本月、本年、其他
    //SessionGroup为键 可保证列表的时间顺序
    QMap<SessionGroup,QVector<StSession>> mapSessionInfo;

    QVector<StSession> vecData = CTableSession::instance()->queryHistory();
    //@TODO 读出历史会话
    foreach(StSession info,vecData) {
        QDateTime dtClose = CHelper::dateTime(info.close_time);
        int nSec = dtClose.daysTo(QDateTime::currentDateTime());
        if(nSec < 1){
            mapSessionInfo[Today].append(info);
        }
        else if(nSec < 7)
        {
            mapSessionInfo[ThisWeek].append(info);
        }
        else if(nSec < 30)
        {
            mapSessionInfo[ThisMonth].append(info);
        }
        else if(nSec < 365)
        {
            mapSessionInfo[ThisYear].append(info);
        }
        else
        {
            mapSessionInfo[Other].append(info);
        }
    }

//    qDebug()<<"会话分组:"<<mapSessionInfo;
    //按照组创建界面
    foreach(SessionGroup group,mapSessionInfo.keys()){
        if(mapSessionInfo[group].isEmpty())
            continue ;

        //组名称
        QLabel *pLabelGroupName = new QLabel(ui->widget_sessions);
        pLabelGroupName->setProperty("title","h2");
        pLabelGroupName->setText(mapGroupName.value(group));
        pLabelGroupName->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
        ui->verticalLayout_sessions->addSpacing(15);
        ui->verticalLayout_sessions->addWidget(pLabelGroupName);
        ui->verticalLayout_sessions->addSpacing(5);

        //该组包含的会话
        FlowLayout *pGroupLayout = new FlowLayout(-1,15,15);
        pGroupLayout->setSpacing(15);
        for (int i = 0; i < mapSessionInfo[group].count(); i++) {
            VHistorySession *pVession = new VHistorySession(mapSessionInfo[group].at(i),ui->widget_sessions);

            QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(pVession);
            shadow->setOffset(0,0);
            shadow->setColor(QColor("#10000000"));
            shadow->setBlurRadius(25);
            pVession->setGraphicsEffect(shadow);

            pGroupLayout->addWidget(pVession);
        }

        //加入布局
        ui->verticalLayout_sessions->addLayout(pGroupLayout);
    }

    //@TODO 是否需要检测 无历史显示空
    ui->verticalLayout_sessions->addStretch();
}

void VHistory::on_pushButton_clicked()
{
    //新建会话
    CMainWindow::getInstance()->hSplitSession("");
}

void VHistory::on_pushButton_clear_clicked()
{
    //弹窗
    if(QMessageBox::Ok == QMessageBox::warning(
                this,tr("清空会话"),tr("确定清空所有会话吗？会话数据将被删除。"),
                QMessageBox::Ok,QMessageBox::Cancel)){
        CTableSession::instance()->deleteHistory();
        refreshUI();

        CTableConfig::instance()->updateValue(CTableConfig::SelectedId,"");
    }
}
